$(function () {
    var project_datagrid = $("#project_datagrid");
    var project_client_datagrid = $("#project_client_datagrid");
    var project_dialog = $("#project_dialog");
    var project_form = $("#project_form");
    var project_report_dialog = $("#project_report_dialog");
    var project_report_form = $("#project_report_form");
    var client_dialog = $("#client_dialog");

    $("#queryObject").hide();

    project_datagrid.datagrid({
        fitColumns: true,
        fit: true,
        toolbar: '#project_toolbar',
        url: '/project/list.do?group=0',
        pagination: true,
        striped: true,
        rownumbers: true,
        singleSelect: true,
        columns: [[
            {title: '执行结果',align:"center", field: 'report', width: '200', formatter: function (value, row, index) {
                    var color = "black"
                    var result = "<font color='"+color+"'></font>"
                    if(value){
                        if (value.name == "优"){
                            color = "green"
                        }else if(value.name == "中"){
                            color = "chocolate"
                        }else if(value.name == "差"){
                            color = "red"
                        }
                        value = value.name;
                    }else{
                        value = "";
                    }
                    return "<font color='"+color+"'>"+value+"</font>";
                }
            },
            {title: '计划时间', align:"center",field: 'projectDate', width: '200'},
            {
                title: '创建人',align:"center", field: 'input', width: '200', formatter: function (value, row, index) {
                    return value ? value.realname : "<font color='red'>------</font>";
                }
            },
            {
                title: '客户', align:"center",field: 'client', width: '200', formatter: function (value, row, index) {
                    return value ? value.name : "";
                }
            },
            {title: '计划主题',align:"center", field: 'theme', width: '200'},
            {
                title: '实施方式',align:"center", field: 'method', width: '200', formatter: function (value, row, index) {
                    return value ? value.name : "";
                }
            },
            {title: '备注',align:"center", field: 'remark', width: '200'},
            {title: '详情' ,align:"center", field: 'particular', width: '200' }
        ]]
    })
    //潜在客户列表
    project_client_datagrid.datagrid({
        fit: true,
        url: '/client/list.do?kind=1&group=0',
        pagination: true,
        striped: true,
        rownumbers: true,
        singleSelect: true,
        columns: [[
            {
                title: '负责人',align:"center", field: 'principal', width: '70', formatter: function (value, row, index) {
                    return value ? value.realname : "<font color='red'>------</font>";
                }
            },
            {title: '客户姓名', align:"center",field: 'name', width: '70'},
            {title: '年龄',align:"center", field: 'age', width: '36'},
            {
                title: '性别',align:"center", field: 'gender', width: '36', formatter: function (value, row, index) {
                    var gender = "保密";
                    if (value == 1) {
                        gender = "男";
                    }
                    ;
                    if (value == 2) {
                        gender = "女";
                    }
                    ;
                    return gender
                }
            },
            {title: '电话',align:"center", field: 'tel', width: '100'},
            {title: '邮箱',align:"center", field: 'email', width: '120'},
            {title: '客户地址', align:"center",field: 'address', width: '100'},
            {title: '身份证', align:"center",field: 'card', width: '120' }
        ]]
    })


    project_dialog.dialog({
        width: 550,
        height: 370,
        buttons: '#project_buttons',
        closed: true,
        onClose: function () {
            project_form.form("clear")
        }
    })

    var methodObj = {
        //添加按钮
        add: function () {
            project_dialog.dialog("open");
            project_dialog.dialog('setTitle', '新增计划信息');
        },
        //编辑按钮
        edit: function () {
            var data = project_datagrid.datagrid('getSelected');
            if (!data) {
                $.messager.alert('温馨提示', "至少选择一条信息");
                return;
            }
            if (data.report) {
                $.messager.alert('温馨提示', "请勿选择已评价计划");
                return;
            }
            console.log(data.client.id);
            if(data.method){
                data['method.id'] = data.method.id;
            }
            if(data.client){
                data['client.id'] = data.client.id;
            }
            $("#clientName").textbox("setValue",data.client.name)
            project_form.form('load', data);

            project_dialog.dialog("open");
            project_dialog.dialog('setTitle', '编辑计划信息');
        },
        //删除计划按钮
        delete:function () {
            var data = project_datagrid.datagrid('getSelected');
            if (!data) {
                $.messager.alert('温馨提示', "至少选择一条信息");
                return;
            }
            $.messager.confirm("温馨提示", "你确定要把该计划删除么？", function (r) {
                if (r) {
                    $.get("/project/delete.do", {id: data.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '删除成功', 'info', function () {
                                methodObj.cancel();
                                potent_datagrid.datagrid("reload");
                            });
                        } else {
                            $.messager.alert('温馨提示', data.msg);
                        }
                    },"json")
                }

            })
        },
        //刷新按钮
        reload: function () {
            project_datagrid.datagrid("reload");
        },
        //取消按钮
        cancel: function () {
            project_dialog.dialog("close");
            project_report_dialog.dialog("close");
        },
        //添加保存按钮
        save: function () {
            project_form.form('submit', {
                url: '/project/saveOrUpdate.do',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '保存成功', 'info', function () {
                            methodObj.cancel();
                            project_datagrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert('温馨提示', data.msg);
                    }
                }
            })
        },
        //展示高级查询条件
        showQuery:function () {
            var obj = $("#queryObject");
            if(obj.data("num")){
                $("#queryObject").show();
                obj.data("num", 0);
            }else {
                $("#queryObject").hide();
                obj.data("num", 1);
            }
        },
        //清空高级查询条件
        clearQuery : function () {
            //清空高级查询数据
            $("#beginDate").datebox("setValue","");
            $("#endDate").datebox("setValue","");
            $("#keyword").textbox("setValue","");
            $("#qoCard").textbox("setValue","");
            $("#qoTel").textbox("setValue","");
            $("#qoPri").textbox("setValue","");
            //关闭高级查询div
            $("#queryObject").hide();
            $("#queryObject").data("num", 1);
            //重新加载数据表格
            $("#project_datagrid").datagrid('reload',{});
        },
        //高级查询
        query:function () {
            console.log(111)
            var beginDate = $("#beginDate").textbox("getValue");
            var endDate = $("#endDate").textbox("getValue");
            var client = $("#qoClient").textbox("getValue");
            var input = $("#qoInput").textbox("getValue");
            var theme = $("#qoTheme").textbox("getValue");
            var methodId = $("#methodId").combobox("getValue");

            $("#project_datagrid").datagrid('load', {
                beginDate: beginDate,
                endDate: endDate,
                client: client,
                input : input,
                theme : theme,
                methodId : methodId
            });
        },
        //用户列表选中按钮
        input:function () {
            var data = project_client_datagrid.datagrid('getSelected');
            if (!data) {
                $.messager.alert('温馨提示', "至少选择一条信息");
                return;
            }
            $("#clientId").val(data.id);
            $("#clientName").textbox("setValue",data.name)
            client_dialog.dialog("close");
        },
        //用户列表取消按钮
        inputCancel:function () {
            client_dialog.dialog("close");
        },
        //标记结果
        report : function(){
            var data = project_datagrid.datagrid('getSelected');
            if (!data) {
                $.messager.alert('温馨提示', "至少选择一条信息");
                return;
            }
            if (data.report) {
                $.messager.alert('温馨提示', "请勿重复评分");
                return;
            }
            $("#inputName").textbox("setValue",data.input.realname);
            project_report_form.form('load', data);
            project_report_dialog.dialog("open");
        },
        //标记结果确定按钮
        reportIn:function () {
            project_report_form.form('submit', {
                url: '/project/report.do',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '保存成功', 'info', function () {
                            methodObj.cancel();
                            project_datagrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert('温馨提示', data.msg);
                    }
                }
            })
        }
    }

    client_dialog.dialog({
        title:'请选择潜在客户',
        width: 700,
        height: 450,
        buttons:'#project_client_buttons',
        closed: true,
    })
    project_report_dialog.dialog({
        title:'请选择潜在客户',
        width: 300,
        height: 250,
        buttons:'#project_report_buttons',
        closed: true,
    })


    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObj[cmd]();
    })

    $("#clientList").click(function () {
        client_dialog.dialog("open");
    })


})

